package com.wu.project.admin.service.impl;

import com.wu.project.admin.component.JsonResult;
import com.wu.project.admin.dao.TicketDao;
import com.wu.project.admin.entity.*;
import com.wu.project.admin.service.TicketService;
import com.wu.project.admin.util.CommonResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author xuan
 */
@Service
public class TicketServiceImpl implements TicketService {

    Logger logger = LoggerFactory.getLogger(TicketServiceImpl.class);

    @Autowired
    TicketDao ticketDao;

    @Override
    public JsonResult getTicketList(TicketQueryVO ticketQueryVO) {
        Integer offset = CommonResultUtil.Instance.getOffset(ticketQueryVO.getPageNum(), ticketQueryVO.getPageSize());
        ticketQueryVO.setOffset(offset);

        List<TicketDTO> ticketList = ticketDao.queryTicketList(ticketQueryVO);
        Integer total = ticketDao.queryTicketListSize(ticketQueryVO);
        PageData pageData = new PageData();
        pageData.setPageNum(ticketQueryVO.getPageNum());
        pageData.setPageSize(ticketQueryVO.getPageSize());
        pageData.setTotal(total);
        pageData.setData(ticketList);
        return CommonResultUtil.Instance.success(pageData);
    }

    @Override
    public JsonResult updateTicket(TicketDTO ticketDTO) {
        String text = "满" + ticketDTO.getCouponExplain() + "减" + ticketDTO.getCouponMoeny();
        ticketDTO.setCouponType(text);
        Integer state = ticketDao.updateTicket(ticketDTO);
        if (state > 0) {
            return CommonResultUtil.Instance.success();
        }
        return CommonResultUtil.Instance.error();
    }

    @Override
    public JsonResult addTicket(TicketDTO ticketDTO) {
        String text = "满" + ticketDTO.getCouponExplain() + "减" + ticketDTO.getCouponMoeny();
        ticketDTO.setCouponType(text);
        ticketDTO.setCouponCreatime(new SimpleDateFormat("yyyy-MM-ss HH:mm:ss").format(new Date(System.currentTimeMillis())));

        Integer state = ticketDao.addTicket(ticketDTO);
        if (state > 0) {
            return CommonResultUtil.Instance.success();
        }
        return CommonResultUtil.Instance.error();
    }

    @Override
    public JsonResult ticketRecord(TicketRecordQueryVO ticketRecordQueryVO) {
        Integer offset = CommonResultUtil.Instance.getOffset(ticketRecordQueryVO.getPageNum(), ticketRecordQueryVO.getPageSize());
        ticketRecordQueryVO.setOffset(offset);

        List<TicketRecordDTO> ticketRecordDTOList = ticketDao.queryTicketRecordList(ticketRecordQueryVO);
        Integer total = ticketDao.queryTicketRecordCount(ticketRecordQueryVO);
        PageData pageData = new PageData();
        pageData.setPageSize(ticketRecordQueryVO.getPageSize());
        pageData.setPageNum(ticketRecordQueryVO.getPageNum());
        pageData.setTotal(total);
        pageData.setData(ticketRecordDTOList);
        return CommonResultUtil.Instance.success(pageData);
    }
}
